package com.rika.controller;

import com.rika.pojo.Category;
import com.rika.pojo.PayRecord;
import com.rika.pojo.Product;
import com.rika.response.QueryResult;
import com.rika.response.Result;
import com.rika.service.ProductService;
import com.rika.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProductController {
    @Resource
    private ProductService productService;
    @RequestMapping("getAllProduct")
    public QueryResult selectAllProduct(Integer currentPage,Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<Product> list=productService.selectAllProduct(params);
        System.out.println(list);
        int total=productService.countAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("deleteProduct")
    public Result deleteProduct(String productCode){
        Result result=new Result();
        productService.deleteProduct(productCode);
        result.setMsg("删除成功");
        result.setCode(200);
        return result;
    }
    @RequestMapping("addProduct")
    public Result addProduct(Product product){
        Result result=new Result();
        productService.addProduct(product);
        result.setCode(200);
        result.setMsg("增加成功");
        return result;
    }
    @RequestMapping("selectByProductCode")
    public Result selectByProductCode(String productCode){
        Result result=new Result();
        List<Product> list=productService.selectByProductCode(productCode);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("selectCategoryIdByProductCode")
    public Result selectCategoryIdByProductCode(String productCode){
        Result result=new Result();
        List<Integer> list=productService.selectCategoryByProductCode(productCode);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("updateProduct")
    public Result updateProduct(Product product){
        Result result=new Result();
        productService.updateProduct(product);
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("selectAllProductForPo")
    public Result selectAllProductForPo(){
        Result result=new Result();
        List<Product> list=productService.selectAllProductForPo();
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("selectProductByName")
    public Result selectByName(String name){
        Result result=new Result();
        Product list=productService.selectByName(name);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("getAllProductForStore")
    public QueryResult getAllProductForStore(Integer currentPage,Integer pageSize){
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<Product> list=productService.selectProductForStore(params);
        int total=productService.countAll();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("selectProductForStockRecord")
    public Result selectProductForStockRecord(String productCode,String name,Integer min,Integer max){
        Result result=new Result();
        Map<String,Object> params=new HashMap<>();
        params.put("productCode",productCode);
        params.put("name",name);
        params.put("min",min);
        params.put("max",max);
        List<Product> list=productService.selectProductForStockRecord(params);
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("selectProductReport")
    public Result selectProductReport(String date){
        Result result=new Result();
        String[] Date=date.split("~");
        Map<String,Object> params=new HashMap<>();
        params.put("startTime",Date[0].trim());
        params.put("endTime",Date[1].trim());
        List<Product> list=productService.selectProductReport(params);
        result.setCode(200);
        result.setData(list);
        return result;
    }
}
